<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-AAS.
 *
 * PHP-AAS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-AAS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-AAS; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-aas
 * @subpackage  users
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

require_once("aas/AbstractUser.php");

/**
 * <p>Concrete implementation of {@link User} for the
 * {@link MemoryUserDatabase} implementation of {@link UserDatabase}.</p>
 */

class MemoryUser extends AbstractUser {


    // ----------------------------------------------------------- Constructors


    /**
     * Package-private constructor used by the factory method in
     * {@link MemoryUserDatabase}.
     *
     * @param database The {@link MemoryUserDatabase} that owns this user
     * @param username Logon username of the new user
     * @param password Logon password of the new user
     * @param fullName Full name of the new user
     */
    public function __construct(MemoryUserDatabase $database, $username,
               $password, $fullName) {
        $this->database = $database;
        $this->setUsername($username);
        $this->setPassword($password);
        $this->setFullName($fullName);

    }


    // ----------------------------------------------------- Instance Variables


    /**
     * The {@link MemoryUserDatabase} that owns this user.
     */
    protected $database = null;


    /**
     * The set of {@link Group}s that this user is a member of.
     */
    protected $groups = array();


    /**
     * The set of {@link Role}s associated with this user.
     */
    protected $roles = array();


    // ------------------------------------------------------------- Properties


    /**
     * Return the set of {@link Group}s to which this user belongs.
     */
    public function getGroups() {
        return $this->groups;
    }


    /**
     * Return the set of {@link Role}s assigned specifically to this user.
     */
    public function getRoles() {
        return $this->roles;
    }


    /**
     * Return the {@link UserDatabase} within which this User is defined.
     */
    public function getUserDatabase() {
        return ($this->database);

    }


    // --------------------------------------------------------- Public Methods


    /**
     * Add a new {@link Group} to those this user belongs to.
     *
     * @param group The new group
     */
    public function addGroup(Group $group) {
        if (!in_array($group, $this->groups)) {
            $this->groups[] = $group;
        }
    }


    /**
     * Add a new {@link Role} to those assigned specifically to this user.
     *
     * @param role The new role
     */
    public function addRole(Role $role) {
        if (!in_array($role, $this->roles)) {
            $this->roles[] = $role;
        }
    }


    /**
     * Is this user in the specified group?
     *
     * @param group The group to check
     */
    public function isInGroup(Group $group) {
        return in_array($group, $this->groups);
    }


    /**
     * Is this user specifically assigned the specified {@link Role}?  This
     * method does <strong>NOT</strong> check for roles inherited based on
     * {@link Group} membership.
     *
     * @param role The role to check
     */
    public function isInRole(Role $role) {
        return in_array($role, $this->roles);
    }


    /**
     * Remove a {@link Group} from those this user belongs to.
     *
     * @param group The old group
     */
    public function removeGroup(Group $group) {
        $key = array_search($group, $this->groups);
        if($key !== false)
            unset($this->groups[$key]);
    }


    /**
     * Remove all {@link Group}s from those this user belongs to.
     */
    public function removeGroups() {
        $this->groups = array();
    }


    /**
     * Remove a {@link Role} from those assigned to this user.
     *
     * @param role The old role
     */
    public function removeRole(Role $role) {
        $key = array_search($role, $this->roles);
        if($key !== false)
            unset($this->roles[$key]);
    }


    /**
     * Remove all {@link Role}s from those assigned to this user.
     */
    public function removeRoles() {
        $this->roles = array();
    }


    /**
     * <p>Return a String representation of this user in XML format.</p>
     *
     * <p><strong>IMPLEMENTATION NOTE</strong> - For backwards compatibility,
     * the reader that processes this entry will accept either
     * <code>username</code> or </code>name</code> for the username
     * property.</p>
     */
    public function __toString() {

        $sb = "<user username=\"";
        $sb .= $this->username;
        $sb .= "\" password=\"";
        $sb .= $this->password;
        $sb .= "\"";
        if ($this->fullName !== null) {
            $sb .= " fullName=\"";
            $sb .= $this->fullName;
            $sb .= "\"";
        }
        if (count($this->groups) > 0) {
            $sb .= " groups=\"";
            for($i=0; $i < count($this->groups); $i++) {
                $sb .= $this->groups[$i]->getGroupName();
                if($i < count($this->groups) - 1)
                    $sb .= ",";

            }
            $sb .= "\"";
        }
        if (count($this->roles) > 0) {
            $sb .= " roles=\"";
            for($i=0; $i < count($this->roles); $i++) {
                $sb .= $this->roles[$i]->getRoleName();
                if($i < count($this->roles) - 1)
                    $sb .= ",";

            }
            $sb .= "\"";
        }
        $sb .= "/>";
        return ($sb);

    }


}
?>